package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 陈安
 * @Date: 2020/12/13$ 20:50$
 * @Description: $todo$
 * @Version 1.0
 */
@Service
public class PageServiceImpl implements PageService {
    @Value("${pagepath}")
    private String pagepath;

    //这个就是完成静态化页面的最终输出的
    @Autowired
    private TemplateEngine templateEngine;

    @Override
    public void generationHtml(String spuId) {
        //获取context对象,存储我们页面的相关数据的
        Context context = new Context();
        //获取静态化页面的相关数据
        Map<String, Object> itemData = this.getItemDate(spuId);
        context.setVariables(itemData);

        //获取商品页面的的存储位置
        File dir=new File(pagepath);
        //判断当前存储页面的文件夹是否存在,如果不存在,则创建
        if(!dir.exists()){
            dir.mkdirs();
        }
        //定义输出流,完成文件的上传
        File file=new File(dir+"/"+spuId+".html");
        Writer out=null;
        try {
            out=new PrintWriter(file);
            //生成静态页面
            //1:模板名称 2:context对象也就是我们的数据 3:输出流也就是页面放到哪
            templateEngine.process("item",context,out);
        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //关闭流

    }

    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private SkuFeign skuFeign;
    @Autowired
    private CategoryFeign categoryFeign;

    //获取静态化页面的相关数据
    private Map<String, Object> getItemDate(String spuId) {
        Map<String, Object> resultMap = new HashMap<>();
        //获取spu
        Spu spu = spuFeign.findSpuById(spuId).getData();
        resultMap.put("spu", spu);

        //图片信息
        if (spu != null) {
            if (StringUtils.isNotEmpty(spu.getImages())) {
                resultMap.put("imagesList", spu.getImages().split(","));
            }
        }

        //商品的分类信息
        Category category1 = categoryFeign.findById(spu.getCategory1Id()).getData();
        resultMap.put("category1",category1);
        Category category2 = categoryFeign.findById(spu.getCategory1Id()).getData();
        resultMap.put("category2",category2);
        Category category3 = categoryFeign.findById(spu.getCategory1Id()).getData();
        resultMap.put("category3",category3);

        //sku的相关信息
        List<Sku> skuList = skuFeign.findListBySpuId(spuId);
        resultMap.put("skuList",skuList);

        //获取商品的规格信息
        resultMap.put("specificationList", JSON.parseObject(spu.getSpecItems(),Map.class));

        return resultMap;
    }
}
